import { getFilePath } from "./common";
import { MessageTypes } from "./constants";

export function formatMessageList(messageList = []) {
  const results = [];
  messageList.forEach((item) => {
    let payloadData = null;
    if (typeof item.payload.data === "string") {
      payloadData = JSON.parse(item.payload.data);
    } else {
      payloadData = item.payload.data;
    }

    if (MessageTypes.includes(payloadData.type)) {
      results.push({
        ...item,
        avatar: getFilePath(item.avatar),
        m_type: `${payloadData.type.replace(/^./, (c) =>
          c.toUpperCase()
        )}Message`,
        m_data: payloadData.value,
        payload: {
          ...item.payload,
          data: payloadData,
        },
      });
    }
  });
  return results;
}
